//
//  HWMygameYouXiViewController.m
//  HWPromoteProjectAPP
//
//  Created by 陈道斌 on 2020/3/2.
//  Copyright © 2020 陈道斌. All rights reserved.
//

#import "HWMygameYouXiViewController.h"
#import "HWPromoteGameModel.h"
#import "HWMyGameYouXiCollectionViewCell.h"
#import "HWApplyGameViewController.h"  // 申请游戏
#import "HWGameDetailsViewController.h"


@interface HWMygameYouXiViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>
@property(strong,nonatomic) UICollectionView *maincollectionView;
@property(strong,nonatomic) NSString *p;
@property(strong,nonatomic) NSString *max_p;
@property(strong,nonatomic) NSMutableArray *dataMuArr;


@end

@implementation HWMygameYouXiViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    // 设置UI
    [self setupUI];
    // 添加控件
    [self addControlsView];
     
    // 添加刷新控件
    [self addrefreshControl];
    
    
    
    
}

// 设置UI
-(void)setupUI {
      
    self.view.backgroundColor = beijingyanSe;
    self.navigationItem.title = @"我的游戏";
    
    
//      UIButton *wangjibutton = [UIButton buttonWithType:UIButtonTypeCustom];
//        wangjibutton.frame = CGRectMake(20,kScreenH - CF_TabbarSafeBottomMargin-10 -44,kScreenW-40, 44);
//        [wangjibutton setTitle:@"申请游戏" forState:UIControlStateNormal];
//      wangjibutton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
//      wangjibutton.contentVerticalAlignment = UIControlContentHorizontalAlignmentCenter;
//        [wangjibutton setTitleColor:RGB(255, 255, 255) forState:UIControlStateNormal];
//      //  wangjibutton.titleLabel.font = [UIFont systemFontOfSize:14];
//        [wangjibutton addTarget:self action:@selector(jiesuanbuttonTap) forControlEvents:UIControlEventTouchUpInside];
//        wangjibutton.backgroundColor = RGB(85, 89, 234);
////        wangjibutton.layer.cornerRadius = 11;
////        wangjibutton.layer.masksToBounds  = YES;
////      wangjibutton.layer.borderWidth = 1;
////      wangjibutton.layer.borderColor = RGB(255, 255, 255).CGColor;
////
//       // wangjibutton.backgroundColor = [UIColor redColor];
//      [self.view addSubview:wangjibutton];

    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(20, kScreenH - CF_TabbarSafeBottomMargin-10 -44,kScreenW-40, 44);
    [button setTitle:@"申请游戏" forState:UIControlStateNormal];
    button.backgroundColor = RGB(85, 89, 234);
    
    [button setTitleEdgeInsets:UIEdgeInsetsMake(8,5, 0.0,0.0)];
    [button addTarget:self action:@selector(jiesuanbuttonTap) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];
    
    
    
    
    
    
    
    
    
    
    
}

// 添加控件
- (void)addControlsView {
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        layout.minimumLineSpacing = 18;
        CGFloat juanju  = (kScreenW - 20*CFLScaleValue*2 - 96*3*CFLScaleValue)/2;
        layout.minimumInteritemSpacing = juanju-1;
    
     //   layout.headerReferenceSize = CGSizeMake(kScreenW, 40);  //设置headerView大小
        self.maincollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, kScreenW ,kScreenH-CF_TabbarSafeBottomMargin-44) collectionViewLayout:layout];
        // self.maincollectionView.showsHorizontalScrollIndicator = NO;
        self.maincollectionView.delegate = self;
        self.maincollectionView.dataSource = self;
        self.maincollectionView.alwaysBounceVertical = YES;
        self.maincollectionView.backgroundColor = beijingyanSe;
       // self.maincollectionView.contentInset = UIEdgeInsetsMake(0, 15, 0, 15);
        self.maincollectionView.contentInset = UIEdgeInsetsMake(0, 20*CFLScaleValue, 0, 20*CFLScaleValue);
      //  [self.maincollectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"HeaderView"];  //  一定要设置
        [self.maincollectionView registerClass:[HWMyGameYouXiCollectionViewCell class] forCellWithReuseIdentifier:@"cellID"];
     //   [self.maincollectionView  registerNib:[UINib nibWithNibName:@"HWJinXuanCell" bundle:nil] forCellWithReuseIdentifier:@"cellID"];
        self.maincollectionView.userInteractionEnabled = YES;
        [self.view  addSubview:self.maincollectionView];
    
    
    
    
    
}



- (void)addrefreshControl {
   
    self.maincollectionView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(underRefresh)];
    
   [self.maincollectionView.mj_header beginRefreshing];
    
    self.maincollectionView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(onRefresh)];
    
    
    
}




// 下拉刷新
- (void)underRefresh {
      self.p = @"1";
      NSString *tStr = [NSString stringWithFormat:@"%ld",[WMUtil getTimeSp]];
      NSString *uid =  [HWLocallyStoredData getLoginUserID];
      NSString *signStr = [NSString stringWithFormat:@"%@%@%@",uid,tStr,@"adesxloksy13s98adv"];
      NSString  *signStrMd5  =   [WMUtil md5HexDigest:signStr];
      NSDictionary *parametes = @{              @"ios" : @"1",
                                           @"p" :self.p,
                                           @"ct" : @"app",
                                            @"ac" : @"mygames",
                                            @"t" : tStr,
                                            @"uid":uid,
                                            @"sign" :signStrMd5
                                            };
    
    
            
            
        [HWWebUtils doGetWithParameters:parametes progress:nil success:^(id response) {
                      
                       [self.maincollectionView.mj_header endRefreshing];
                       [self.maincollectionView.mj_footer endRefreshing];

                      NSString *codeStr =  [NSString stringWithFormat:@"%@",response[@"code"]];
                      if ([codeStr intValue] == 1) {
                          
                          
                          self.p = [NSString stringWithFormat:@"%@",response[@"p"]];
                          
                          self.max_p = [NSString stringWithFormat:@"%@",response[@"max_p"]];
                          self.dataMuArr = [HWPromoteGameModel  mj_objectArrayWithKeyValuesArray:response[@"data"]];
                          [self.maincollectionView reloadData];
                           NSLog(@"%@",response);
                    
                          
                           
                      } else {
                          
                        [MBProgressHUD showError:response[@"message"]];
                                        
                    
                          
                      }
                      
                 
                      
                  } failure:^(NSString *error) {
                     // [SVProgressHUD dismiss];
                      [self.maincollectionView.mj_header endRefreshing];
                      [self.maincollectionView.mj_footer endRefreshing];
                      
                      
//                       [MBProgressHUD showSuccess:@"登录失败"];
//                       [MBProgressHUD hideHUD];
                      
                      
                  }];
            
        
    
      
    
    


}





// 上拉加载

- (void)onRefresh {
    
    int p = [self.p intValue];
                   
    int max_p = [self.max_p intValue];
                   
     if (p > max_p) {
         
          [self.maincollectionView.mj_header endRefreshing];
          [self.maincollectionView.mj_footer endRefreshing];

        [MBProgressHUD showError:@"没有更多数据，请待会在上拉加载"];
                       
        return ;
                       
      }
    
        NSString *tStr = [NSString stringWithFormat:@"%ld",[WMUtil getTimeSp]];
          NSString *uid =  [HWLocallyStoredData getLoginUserID];
          NSString *signStr = [NSString stringWithFormat:@"%@%@%@",uid,tStr,@"adesxloksy13s98adv"];
          NSString  *signStrMd5  =   [WMUtil md5HexDigest:signStr];
          NSDictionary *parametes = @{              @"ios" : @"1",
                                               @"p" :self.p,
                                               @"ct" : @"app",
                                                @"ac" : @"mygames",
                                                @"t" : tStr,
                                                @"uid":uid,
                                                @"sign" :signStrMd5
                                                };
                
                
                    [HWWebUtils doGetWithParameters:parametes progress:nil success:^(id response) {
                          
                           [self.maincollectionView.mj_header endRefreshing];
                           [self.maincollectionView.mj_footer endRefreshing];

                          NSString *codeStr =  [NSString stringWithFormat:@"%@",response[@"code"]];
                          if ([codeStr intValue] == 1) {
                              
                              
                              self.p = [NSString stringWithFormat:@"%@",response[@"p"]];
                              self.max_p = [NSString stringWithFormat:@"%@",response[@"max_p"]];
                              
                              
                              NSMutableArray *mutableArr = [HWPromoteGameModel  mj_objectArrayWithKeyValuesArray:response[@"data"]];
                              for (int i = 0; i < mutableArr.count; i++) {


                                  [self.dataMuArr addObject:mutableArr[i]];

                               }
                              
                              
                              [self.maincollectionView reloadData];
                         
                                NSLog(@"%@",response);
                              
                    
                              
                               
                          } else {
                              
                            [MBProgressHUD showError:response[@"message"]];
                                            
                        
                              
                          }
                          
                     
                          
                      } failure:^(NSString *error) {
                         // [SVProgressHUD dismiss];
                          [self.maincollectionView.mj_header endRefreshing];
                          [self.maincollectionView.mj_footer endRefreshing];
                          
                          
    //                       [MBProgressHUD showSuccess:@"登录失败"];
    //                       [MBProgressHUD hideHUD];
                          
                          
                      }];
                
            
    
    
    
        
                          
    
}
















- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    return self.dataMuArr.count;
}


- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    HWMyGameYouXiCollectionViewCell *cell =  [collectionView dequeueReusableCellWithReuseIdentifier:@"cellID" forIndexPath:indexPath];
  //   cell.backgroundColor =[UIColor yellowColor];
//    HWGameListModel *mode = self.dataMuArr[indexPath.row+2];
//    cell.model = mode;

 //   cell.dataModel = self.dataMuArr[indexPath.row];
    cell.layer.cornerRadius = 12;
    cell.layer.masksToBounds = YES;
    
    
    
    HWPromoteGameModel *model =  self.dataMuArr[indexPath.row];
    cell.model = model;
     
    
    if (indexPath.row%2 == 0) {
        cell.backgroundColor = [UIColor whiteColor];
    } else {
        
        cell.backgroundColor = [UIColor whiteColor];
    }
    return cell;
    
    
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    return CGSizeMake(96*CFLScaleValue,86*CFLScaleValue+29+37);
    
    
    
    
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    DLog(@"12334");
    
//    HWGameDetailsViewController *gameDetails = [[HWGameDetailsViewController alloc]init];
//
//    HWGameListModel *mode = self.dataMuArr[indexPath.row+2];
//    gameDetails.gameId = mode.id;
//
    
 //   [self.navigationController pushViewController:gameDetails animated:YES];
    
    HWGameDetailsViewController *gameDetails = [[HWGameDetailsViewController alloc]init];

    HWPromoteGameModel *mode =  self.dataMuArr[indexPath.row];
    gameDetails.idStr = mode.gid;
    [self.navigationController pushViewController:gameDetails animated:YES];
    
    
    
    

    
    
}










#pragma mark ----- 申请游戏
- (void)jiesuanbuttonTap {
    
    
    HWApplyGameViewController *ApplyGame = [[HWApplyGameViewController alloc]init];
    [self.navigationController pushViewController:ApplyGame animated:YES];
    NSLog(@"djcdjkfki");
    
    
}


@end
